package user.springboot.rabbitmq.a0base;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMqUtil {
	// 工作模式，队列名称 。多个消费端消费同一个队列中的消息，队列采用轮询的方式将消息是平均发送给消费者；
	public static final String WORK_QUEUE = "work_queue";


	public static Connection connectionGet() throws IOException, TimeoutException {
		//1.创建连接工厂(设置RabbitMq的连接参数)
		ConnectionFactory connectionFactory = new ConnectionFactory();
		//设置主机:默认:localhost
		connectionFactory.setHost("192.168.31.65");
		//连接端口:默认:5672
		connectionFactory.setPort(5672);
		//虚拟主机:默认:/
		connectionFactory.setVirtualHost("/");
		//用户名:默认:guest
		connectionFactory.setUsername("admin");
		//密码:默认:guest
		connectionFactory.setPassword("123456");
		//2.创建连接
		Connection connection = connectionFactory.newConnection();
		//返回
		return connection;
	}
	
	public static void resourceClose(Channel channel) {
		try {
			channel.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
	}
	
	public static void resourceClose(Connection connection) {
		try {
			connection.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
